
<template>
  <div class="execution">

    <basic-container>
      <avue-crud
        ref="crud"
        :page="page"
        :data="tableData"
        :permission="permissionList"
        :table-loading="tableLoading"
        :option="tableOption"
        @on-load="getList"
        @search-change="searchChange"
        @search-reset="searchReset"
        @refresh-change="refreshChange"
        @selection-change="selectionChange"
	:cell-style="cellStyle"
      >
        <template slot="menuRight">
          <el-button
            v-if="permissionList.confirm"
            type="primary"
            size="small"
            @click="handleConfirm"
          >确认</el-button>
        </template>
      </avue-crud>
    </basic-container>
  </div>
</template>

<script>
import { fetchList, confirm } from '@/api/rca/viewckmarketingticketelec'
import { tableOption } from '@/const/crud/rca/viewckmarketingticketelec'
import { mapGetters } from 'vuex'
import { dateFormat } from '@/util/date';

export default {
  name: 'viewckmarketingticketelec',
  data () {
    return {
      searchForm: {
        tradeDate: dateFormat(new Date(), 'yyyy-MM-dd'),
      },
      tableData: [],
      page: {
        total: 0, // 总页数
        currentPage: 1, // 当前页数
        pageSize: 20 // 每页显示多少条
      },
      tableLoading: false,
      tableOption: tableOption,

      selection: [],
    }
  },
  computed: {
    ...mapGetters(['permissions']),
    permissionList () {
      return {
        confirm: this.vaildData(this.permissions.rca_swckelectronicreceipt_ckmte_confirm, false),
      };
    }
  },
  methods: {
     cellStyle({ row, column, rowIndex,columnIndex }) {     
      if(12==columnIndex){
        if (row.diffTransferTicket < 0) {
          return {
            color: "red",
          };
        }else if (row.diffTransferTicket > 0) {
          return {
            color: "green",
          };
        }
      }  
      if(13==columnIndex){
        if (row.diffGroupSell< 0) {
          return {
            color: "red",
          };
        }else if (row.diffGroupSell > 0) {
          return {
            color: "green",
          };
        }
      }  
    },
    getList (page, params) {
      this.tableLoading = true
      const { tradeDate } = this.searchForm
      const startDate = dateFormat(new Date(tradeDate), 'yyyy-MM-') + '01';
      const endDate = dateFormat(new Date(tradeDate), 'yyyy-MM-') + '31';;
      fetchList(Object.assign({
        current: page.currentPage,
        size: page.pageSize
      }, params, this.searchForm, {
        startDate,
        endDate
      })).then(response => {
        this.tableData = response.data.data
        this.page.total = response.data.data.total
        this.tableLoading = false
      }).catch(() => {
        this.tableLoading = false
      })
    },
    handleConfirm: function () {
      confirm({
        mtEleList: this.selection
      }).then(data => {
        this.$message.success('确认成功')
        done()
        this.getList(this.page)
      }).catch(() => {
        loading();
      });
    },
    selectionChange (list) {
      this.selection = list;
    },
    async searchChange (form, done) {
      this.searchForm = { ...form };
      try {
        await this.getList(this.page, this.searchForm);
      } finally {
        done();
      }
    },
    refreshChange () {
      this.getList(this.page)
    },
    searchReset () {

      this.page.currentPage = 1
      this.getList(this.page);
    },
  }
}
</script>
